package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        //判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null){
            Map map = new HashMap();
            map.put("islike", false);
            map.put("isfollow", false);
            map.put("isunlike", false);
            map.put("isconllection", false);
            return ResponseResult.okResult(map);
        }
        //如果登录查询用户实体
        ApBehaviorEntry ap = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(),null);
        //根据实体查询是否点赞当前文章
        boolean islike = IsArticlelikes(ap.getId(),dto);
        //根据实体查询是否不喜欢当前文章
        boolean iscollection = iscollection(ap.getId(),dto);
        //根据实体查询是否收藏文章
        boolean isunlike = isunlike(ap.getId(),dto);
        //根据实体查询是否管制
        boolean isfollow = isfollow(user.getId(),dto);
        Map map = new HashMap();
        map.put("islike", islike);
        map.put("isfollow", isfollow);
        map.put("isunlike", isunlike);
        map.put("iscollection", iscollection);
        return ResponseResult.okResult(map);
    }

    @Autowired
    RedisTemplate<String,String> redisTemplate;
    /**
     * 判断是否关注
     * @param id
     * @param dto
     * @return
     */
    private boolean isfollow(Integer id, ArticleBehaviorDTO dto) {
        Double score = redisTemplate.opsForZSet()
                .score(UserRelationConstants.FOLLOW_LIST + id, String.valueOf(dto.getAuthorApUserId()));
        if(score!=null){
            return true;
        }
        return false;
    }

    private boolean isunlike(String id, ArticleBehaviorDTO dto) {
        //查询不喜欢数据
        Query query = Query.query(Criteria.where("entryId").is(id).and("articleId").is(dto.getArticleId()).and("type").is((short)0));
        ApUnlikesBehavior one = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        if (one != null){
            return true;
        }
        return false;
    }

    /**
     * 是否收藏
     * @param id
     * @param dto
     * @return
     */
    private boolean iscollection(String id, ArticleBehaviorDTO dto) {
        //根据实体id,文章id查询收藏信息
        Query query = Query.query(Criteria.where("entryId").is(id).and("articleId").is(dto.getArticleId()).and("type").is((short)0));
        ApCollection one = mongoTemplate.findOne(query, ApCollection.class);
        if (one != null){
            System.out.println("数据存在,已收藏");
            return true;
        }
        System.out.println("数据不存在,未收藏");

        return false;
    }

    /**
     * 是否点赞当前文章
     * @param id
     * @param dto
     * @return
     */
    private boolean IsArticlelikes(String id, ArticleBehaviorDTO dto) {
        Query query = Query.query(Criteria.where("entryId").is(id).and("articleId").is(dto.getArticleId())
                    .and("type").is((short)0).and("operation").is(0));
        ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if (one != null){
            return true;
        }
        return false;
    }
}
